package com.fcy.teamwork.business.checkapply

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.jpush.im.android.api.JMessageClient
import cn.jpush.im.android.api.callback.GetUserInfoCallback
import cn.jpush.im.android.api.model.UserInfo
import com.fcy.teamwork.App
import com.fcy.teamwork.base.BaseRVAdapter
import com.fcy.teamwork.base.toJson
import com.fcy.teamwork.bean.Apply
import com.fcy.teamwork.business.self.SelfInfoAct
import com.fcy.teamwork.databinding.ItemApplyBinding
import com.fcy.teamwork.pojo.JobApply
import com.fcy.teamwork.pojo.JobApplyDetail

class ApplicationAdapter : BaseRVAdapter<JobApplyDetail, ItemApplyBinding>() {
    override fun createBinding(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ItemApplyBinding = ItemApplyBinding.inflate(inflater, parent, false)

    var onAcceptClick: (JobApplyDetail, Int) -> Any? = { _, _ -> }
    var onRefuseClick: (JobApplyDetail, Int) -> Any? = { _, _ -> }
    override fun bindView(holder: VH, position: Int, data: JobApplyDetail) {
        (holder.binding as? ItemApplyBinding)?.apply {
            tvApplyFor.text = data.projectHint
            tvNikeName.text = data.applicant.nikeName
            pvPortrait.setOnClickListener {
                Intent(root.context, SelfInfoAct::class.java).apply {
                    putExtra("canSenMsg", true)
                    putExtra("userName", data.applicant.personId)
                    putExtra("userInfo", data.applicant.toJson())
                    root.context.startActivity(this)
                }
            }
            if (data.jobApply.isResolved == 0 && data.job.ownerId == App.person.personId) {
                llActionBar.visibility = View.VISIBLE
                tvState.visibility = View.GONE
                btnAgree.setOnClickListener {
                    onAcceptClick(data, position)
                }
                btnRefuse.setOnClickListener {
                    onRefuseClick(data, position)
                }
            } else {
                llActionBar.visibility = View.GONE
                tvState.visibility = View.VISIBLE
                tvState.text = when (data.jobApply.isResolved) {
                    0 -> {
                        "待验证"
                    }

                    1 -> {
                        "已接受"
                    }
                    -1 -> {
                        "已拒绝"
                    }
                    else -> throw IllegalStateException("申请状态出现异常")
                }
            }

        }
    }
}